﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing; 
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Tools = Microsoft.Office.Tools;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools.Word.Extensions;

using CloudOfficeEngine;

using GoogleConnector;
using Utilities;

using Microsoft.Win32;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

//using OfficeInCloudWord2007.Proxy;

namespace OfficeInCloudWord2007
{
    public partial class ThisAddIn
    {
        private COE m_Engine;
        private WordLocalView m_LocalView;
        private Connector m_RemoteConnector;
        private DbManager m_db;
        public MyGDocsForm m_MyGDocsForm;
        public Tools.CustomTaskPane m_MyGDocsPane;
        private bool DisabledMode = true;


        //private static TcpChannel channel;

        private void CommunicationSetup()
        {
            m_LocalView = new WordLocalView(this.Application, m_db);
            m_RemoteConnector = new Connector();
            m_Engine = new COE(m_LocalView, m_RemoteConnector);
        }

        
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            m_db = new DbManager(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Uprise Apps\\Info.mdb");

            //kHKCU
            //
            //First Run
            RegistryKey kHKCU = Registry.CurrentUser;
            RegistryKey kFirstRun;
            try
            {
                kFirstRun = kHKCU.OpenSubKey("Software\\UpriseApps\\OfficeInCloudExcel2007", true);
                if (kFirstRun == null)
                {
                    MessageBox.Show("Office In Cloud for Google Docs (Excel 2007) was not properly installed. \n Please contact Uprise Apps support.", "Office In Cloud Error Message");
                }
                else
                {
                    if (kFirstRun.GetValue("FirstRun").ToString().Equals("1"))
                    {
                        //Registration Wizard
                        Registration regForm = new Registration(m_db);
                        if (DialogResult.OK == regForm.ShowDialog())
                        {
                            DisabledMode = false;
                        }
                    }
                    else DisabledMode = false;
                }
            }
            catch
            {
                MessageBox.Show("Office In Cloud for Google Docs (Excel 2007) was not properly installed. \n Please contact Uprise Apps support.", "Office In Cloud Error Message");
            }

            CommunicationSetup();

            ImageList m_ImageList = new ImageList();
            m_ImageList.ColorDepth = ColorDepth.Depth32Bit;
            m_ImageList.TransparentColor = Color.Transparent;
            m_ImageList.ImageSize = new Size(20, 20);


            m_ImageList.Images.Add(Properties.Resources.UpriseApps);
            m_ImageList.Images.Add(Properties.Resources.MessageQueuing);
            m_ImageList.Images.Add(Properties.Resources.Folder);
            m_ImageList.Images.Add(Properties.Resources.users);
            m_ImageList.Images.Add(Properties.Resources.update1);

            m_MyGDocsForm = new MyGDocsForm(m_LocalView, DisabledMode);
            m_MyGDocsForm.AutoSize = true;
            m_MyGDocsForm.MyGDocsTabControl.ImageList = m_ImageList;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["SignIn"].ImageIndex = 0;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["ShareTab"].ImageIndex = 3;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["Explorer"].ImageIndex = 2;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["UpdateTab"].ImageIndex = 4;

            this.Application.ShowWindowsInTaskbar = true;

            m_MyGDocsPane = this.CustomTaskPanes.Add(m_MyGDocsForm, "Office in Cloud", Application.ActiveWindow);
            //m_MyGDocsPane.Control.
            

            m_MyGDocsPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
            m_MyGDocsPane.DockPositionRestrict =
                Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal;
            m_MyGDocsPane.Width = 350;
            m_MyGDocsPane.Visible = true;

            m_MyGDocsPane.VisibleChanged +=
                new EventHandler(m_MyGDocsPane_VisibleChanged);
            m_MyGDocsPane.DockPositionChanged +=
                new EventHandler(m_MyGDocsPane_DockPositionChanged);

            //this.Application.WindowActivate += 
            //    new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(

            System.Drawing.Size paneSize = new System.Drawing.Size(m_MyGDocsPane.Width, m_MyGDocsPane.Height);
            m_MyGDocsForm.Size = paneSize;
            m_MyGDocsPane.Visible = true;

            //Let's open the communication channel
            //channel = new TcpChannel(9999);
            //ChannelServices.RegisterChannel(channel, false);

            //Let's publish the application object
            //UIProxy uiProxy = UIProxy.Instance;
            //RemotingServices.Marshal(uiProxy, "UIProxy");
           
        }


        public void AddTaskPane()
        {
            ImageList m_ImageList = new ImageList();
            m_ImageList.ColorDepth = ColorDepth.Depth32Bit;
            m_ImageList.TransparentColor = Color.Transparent;
            m_ImageList.ImageSize = new Size(20, 20);


            m_ImageList.Images.Add(Properties.Resources.UpriseApps);
            m_ImageList.Images.Add(Properties.Resources.MessageQueuing);
            m_ImageList.Images.Add(Properties.Resources.Folder);
            m_ImageList.Images.Add(Properties.Resources.users);
            m_ImageList.Images.Add(Properties.Resources.update1);


            m_MyGDocsForm = new MyGDocsForm(m_LocalView, DisabledMode);
            m_MyGDocsForm.AutoSize = true;
            m_MyGDocsForm.MyGDocsTabControl.ImageList = m_ImageList;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["SignIn"].ImageIndex = 0;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["ShareTab"].ImageIndex = 3;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["Explorer"].ImageIndex = 2;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["UpdateTab"].ImageIndex = 4;

            this.Application.ShowWindowsInTaskbar = true;

            m_MyGDocsPane = this.CustomTaskPanes.Add(m_MyGDocsForm, "Office in Cloud", Application.ActiveWindow);
            //m_MyGDocsPane.Control.

            m_MyGDocsPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
            m_MyGDocsPane.DockPositionRestrict =
                Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal;
            m_MyGDocsPane.Width = 350;
            m_MyGDocsPane.Visible = true;

            m_MyGDocsPane.VisibleChanged +=
                new EventHandler(m_MyGDocsPane_VisibleChanged);
            m_MyGDocsPane.DockPositionChanged +=
                new EventHandler(m_MyGDocsPane_DockPositionChanged);

            //this.Application.WindowActivate += 
            //    new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(

            m_MyGDocsForm.button1_Click(this, null); 

            System.Drawing.Size paneSize = new System.Drawing.Size(m_MyGDocsPane.Width, m_MyGDocsPane.Height);
            m_MyGDocsForm.Size = paneSize;
            m_MyGDocsPane.Visible = true;

            //Let's open the communication channel
            //channel = new TcpChannel(9999);
            //ChannelServices.RegisterChannel(channel, false);

            //Let's publish the application object
            //UIProxy uiProxy = UIProxy.Instance;
            //RemotingServices.Marshal(uiProxy, "UIProxy");
           
        }

        public void m_MyGDocsPane_DockPositionChanged(object sender, EventArgs e)
        {
            Microsoft.Office.Tools.CustomTaskPane taskPane =
                sender as Microsoft.Office.Tools.CustomTaskPane;

            if (taskPane != null)
            {
                if (taskPane.DockPosition ==
                    Office.MsoCTPDockPosition.msoCTPDockPositionFloating)
                {
                    taskPane.Height = 500;
                    taskPane.Width = 500;
                }
                System.Drawing.Size paneSize = new System.Drawing.Size(taskPane.Width, taskPane.Height);
                m_MyGDocsForm.Size = paneSize;
            }
        }


        public void m_MyGDocsPane_VisibleChanged(object sender, EventArgs e)
        {
            Microsoft.Office.Tools.CustomTaskPane taskPane =
                sender as Microsoft.Office.Tools.CustomTaskPane;

            if (taskPane != null)
            {
                if (taskPane.Visible)
                {
                    taskPane.DockPosition =
                        Office.MsoCTPDockPosition.msoCTPDockPositionRight;
                }
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
